<?php
require_once('Common/Strings.php');
require_once('/View/RESTfulView.php');
require_once('/Model/ProducerModel.php');
class MasterController{
		
	//Private RESTful Model
	private $RFView;
	private $Producer;
	
	/**
	 * Constructor, will Initialize private RFModel, 
	 * private PositionModel and private ProducerModel, both injected with param-DB
	 * @access Public
	 * @param $db connection
	 * @return-
	 */
	function __construct(DBConnection $a_DB){
		$this->RFView = new RESTfulView(); 
		$this->Producer = new ProducerModel($a_DB, $this->RFView->getRequestResponseType());
	}
	
	/**
	 * Initialize function
	 * @access Public
	 * @param -
	 * @return will return RESTful result
	 */
	function init(){
		
		if(!$this->RFView->validRequestedResource()){
			return $this->Producer->setBadRequestHeader("Resource not found!", "404 NotFound");
		}
		
		switch($this->RFView->getRequestType()){
			
			case Strings::GET:
				
				if($this->RFView->getRequestedProducerId() != null){
					return ($this->Producer->getProducerById($this->RFView->getRequestedProducerId()));
				}
				else{
					return ($this->Producer->getAllProducers());
				}
				break;
				
			case Strings::POST:
				
				if($this->RFView->getPostedData() != null){
					return ($this->Producer->createProducer($this->RFView->getPostedData()));
				}
				else{
					return  $this->Producer->setBadRequestHeader("Inclompleat Data provided");
				}
				break;
				
			case Strings::PUT:
				
				$data = $this->RFView->getPostedData();
				
				if($data != null){
					return ($this->Producer->updateProducer($this->RFView->getRequestedProducerId(), $data));
				}
				else{
					return  $this->Producer->setBadRequestHeader("Inclompleat Data provided");
				}
				break;
				
			case Strings::DELETE:
				
				if($this->RFView->getRequestedProducerId() != null){
					return ($this->Producer->deleteProducerById($this->RFView->getRequestedProducerId()));
				}
				else{
					return  $this->Producer->setBadRequestHeader("Inclompleat Data provided");
				}
				break;
				
			default:
				return $this->Producer->setBadRequestHeader();
				break;
		}
	}
}
?>